// bookgo/hsculture/say/say.js
var server = require('../../../utils/server.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        messageShow1: false,
        messageShow2: false,
        messageShow3: false,
        openid: '',
        messageList: [],
        messageDetail: '',
        message: '',
        pid: ''
    },

    // 我要留言
    leaveMessage: function () {
        var that = this;
        that.setData({
            messageList: '',
            messageShow1: true,
            messageShow2: false,
            messageShow3: false
        })
    },
    // 留言详细页
    show: function (e) {
        console.log(e.currentTarget.dataset.id);
        var id = e.currentTarget.dataset.id;
        var that = this;
        that.setData({
            pid: id,
        })
        wx.getUserInfo({
            success: function (res) {
                console.log(res.userInfo.avatarUrl);
                that.setData({
                    avatarUrl: res.userInfo.avatarUrl
                });
                var params = {
                    wxapp_openid: that.data.openid,
                    id: id
                };
                server.postData('article/comment/delete', params, function (res) {
                    console.log(res);
                    var params = {
                        wxapp_openid: that.data.openid,
                        id: id,
                        page: 1,
                        num: 100
                    };
                    server.postData('article/comment/read', params, function (res) {
                        console.log('==')
                        console.log(res);
                        that.setData({
                            messageDetail: res.data.data,
                            messageShow1: false,
                            messageShow2: false,
                            messageShow3: true
                        })
                    })
                })
            }
        })

    },
    // 写的留言
    contentInput: function (e) {
        console.log(e);
        this.setData({
            message: e.detail.value
        })
    },

    // 发送留言
    sendMessage: function () {
        var that = this;
        var params = {
            wxapp_openid: that.data.openid,
            content: that.data.message
        };
        server.postData('article/comment/create', params, function (res) {
            console.log('发送留言成功');
            console.log(res);
            var params = {
                wxapp_openid: that.data.openid,
                id: res.data.data.id,
                page: 1,
                num: 100
            }
            console.log(params);
            server.postData('article/comment/read', params, function (res) {
                console.log('跳到留言详情页');
                console.log(res);
                var messageDetail = res.data.data,
                    pid = res.data.data[0].pid;
                wx.getUserInfo({
                    success: function (res) {
                        console.log(res.userInfo.avatarUrl);
                        that.setData({
                            avatarUrl: res.userInfo.avatarUrl,
                            messageDetail: messageDetail,
                            pid: pid,
                            messageShow1: false,
                            messageShow2: false,
                            messageShow3: true
                        });
                    }
                })
            })

        })
    },

    // 追加留言
    addInput: function (e) {
        console.log(e);
        this.setData({
            message: e.detail.value
        })
    },
    // 发送追加的留言
    sendAddMessage: function () {
        var that = this;
        var params = {
            pid: that.data.pid,
            content: that.data.message,
            wxapp_openid: that.data.openid
        };
        console.log('+++++++++', params);
        server.postData('article/comment/save', params, function (res) {
            console.log('---', res);
            var params = {
                wxapp_openid: that.data.openid,
                id: that.data.pid,
                page: 1,
                num: 50
            };
            server.postData('article/comment/read', params, function (res) {
                console.log('刷新留言详情页')
                console.log(res);
                that.setData({
                    messageDetail: res.data.data,
                    messageShow1: false,
                    messageShow3: true
                })
            })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        wx.login({
            success: function (e) {
                var params = {
                    code: e.code
                };
                server.postData('wxapp/index/login', params, function (res) {
                    console.log('-----------++++++++++++++------------');
                    console.log(res);
                    var openid = res.data.userinfo.openid;
                    that.setData({
                        openid: openid
                    });
                    var params = {
                        wxapp_openid: openid,
                        page: 1,
                        num: 50
                    };
                    server.postData('article/comment/read', params, function (res) {
                        console.log('留言列表', res.data.data);
                        if (res.data.data.length == 0) {
                            that.setData({
                                messageShow1: true,
                                messageShow2: false
                            })
                        } else {
                            that.setData({
                                messageList: res.data.data,
                                messageShow1: false,
                                messageShow2: true
                            })
                        }

                    })
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})